package com.yf.exam.modules.trainingarchives.teacherplanexecution.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Preconditions;
import com.yf.exam.common.entity.Result;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.core.exception.ServiceException;
import com.yf.exam.modules.trainingarchives.organization.dto.OrganizationDTO;
import com.yf.exam.modules.trainingarchives.organization.entity.Organization;
import com.yf.exam.modules.trainingarchives.organization.vo.OrganizationVO;
import com.yf.exam.modules.trainingarchives.studentinfo.dto.resp.StudentInfoRespDTO;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.dto.TeachingPlanExecutionDTO;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.entity.TeachingPlanExecution;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.service.ITeachingPlanExecutionService;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.vo.TeachingPlanExecutionListVO;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.vo.TeachingPlanExecutionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 教学计划执行表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Api(tags={"教学计划执行"})
@RestController
@RequestMapping("/org/api/teaching-plan-execution")
@Slf4j
public class TeachingPlanExecutionController {

    @Resource
    private ITeachingPlanExecutionService  iTeachingPlanExecutionService;
    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody TeachingPlanExecutionDTO reqDTO) {

        try {
            //复制参数
            iTeachingPlanExecutionService.add(reqDTO);
            return Result.ok();
        }catch (ServiceException e){
            return Result.faild(e);
        }
    }

    @ApiOperation(value = "修改")
    @PostMapping (value = "/update")
    public Result update(@RequestBody TeachingPlanExecutionDTO reqDTO) {
        //查询当前机构是否存在
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getId()), "机构id不能为空!");
            //复制参数
            iTeachingPlanExecutionService.update(reqDTO);
            return Result.ok("修改成功！");
        }catch (ServiceException e){
            return Result.faild(e);
        }
    }
    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete")
    public Result delete(@RequestBody BaseIdReqDTO baseIdReqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeachingPlanExecutionController.delete.elProjectReqDTO:{}", JSON.toJSONString(baseIdReqDTO));
            }
            Preconditions.checkNotNull(baseIdReqDTO.getId(), "教学计划id不能为空!");
            Boolean result = iTeachingPlanExecutionService.removeById(baseIdReqDTO.getId());
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TeachingPlanExecutionController.delete.error:{}", e.getMessage(), e);
            return Result.faild("删除教学计划执行失败!");
        }
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public Result<IPage<TeachingPlanExecutionListVO>> paging(@RequestBody PagingReqDTO<TeachingPlanExecutionDTO> reqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeachingPlanExecutionController.paging.dto:{}", JSON.toJSONString(reqDTO));
            }
            //分页查询并转换
            IPage<TeachingPlanExecutionListVO> page = iTeachingPlanExecutionService.paging(reqDTO);
            return Result.ok(page);
        } catch (Exception e) {
            log.error("TeachingPlanExecutionController.paging.error:{}", e.getMessage(), e);
            return Result.faild("查询失败");
        }
    }

    @ApiOperation(value = "查询详情")
    @PostMapping (value = "/get")
    public Result<TeachingPlanExecutionVO> get(@RequestBody BaseIdReqDTO reqDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getId()), "教学计划执行id不能为空!");
        TeachingPlanExecution teachingPlanExecution = iTeachingPlanExecutionService.getById(reqDTO.getId());
        if (teachingPlanExecution == null) {
            return Result.faild("教学计划执行不存在!");
        }
        TeachingPlanExecutionVO vo = new TeachingPlanExecutionVO();
        BeanUtils.copyProperties(teachingPlanExecution,vo);
        return Result.ok(vo);
    }


}
